import { BrowserWindow } from 'electron'

type ShortcutCallback = () => void
type ShortcutMap = Record<string, ShortcutCallback>

interface ShortcutGroup {
  name: string
  shortcuts: {
    mac: ShortcutMap
    windows: ShortcutMap
  }
}

// 定义所有快捷键组
export const shortcutGroups: ShortcutGroup[] = [
  {
    name: '编辑操作',
    shortcuts: {
      mac: {
        'Command+C': (win: BrowserWindow) => win.webContents.copy(),
        'Command+V': (win: BrowserWindow) => win.webContents.paste(),
        'Command+X': (win: BrowserWindow) => win.webContents.cut(),
        'Command+A': (win: BrowserWindow) => win.webContents.selectAll(),
      },
      windows: {
        'Control+C': (win: BrowserWindow) => win.webContents.copy(),
        'Control+V': (win: BrowserWindow) => win.webContents.paste(),
        'Control+X': (win: BrowserWindow) => win.webContents.cut(),
        'Control+A': (win: BrowserWindow) => win.webContents.selectAll(),
      }
    }
  },
  {
    name: '撤销重做',
    shortcuts: {
      mac: {
        'Command+Z': (win: BrowserWindow) => win.webContents.undo(),
        'Command+Shift+Z': (win: BrowserWindow) => win.webContents.redo(),
      },
      windows: {
        'Control+Z': (win: BrowserWindow) => win.webContents.undo(),
        'Control+Y': (win: BrowserWindow) => win.webContents.redo(),
      }
    }
  },
  {
    name: '开发工具',
    shortcuts: {
      mac: {
        'Command+Option+I': (win: BrowserWindow) => win.webContents.toggleDevTools(),
        'Command+Option+J': (win: BrowserWindow) => win.webContents.toggleDevTools(),
        'Command+Option+U': (win: BrowserWindow) => win.webContents.toggleDevTools(),
      },
      windows: {
        'Control+Shift+I': (win: BrowserWindow) => win.webContents.toggleDevTools(),
        'Control+Shift+J': (win: BrowserWindow) => win.webContents.toggleDevTools(),
        'Control+Shift+U': (win: BrowserWindow) => win.webContents.toggleDevTools(),
        'F12': (win: BrowserWindow) => win.webContents.toggleDevTools(),
      }
    }
  },
  {
    name: '页面刷新',
    shortcuts: {
      mac: {
        'Command+R': (win: BrowserWindow) => win.webContents.reload(),
        'Command+Shift+R': (win: BrowserWindow) => win.webContents.reload(),
      },
      windows: {
        'Control+R': (win: BrowserWindow) => win.webContents.reload(),
        'Control+Shift+R': (win: BrowserWindow) => win.webContents.reload(),
        'F5': (win: BrowserWindow) => win.webContents.reload(),
      }
    }
  }
]

// 获取当前平台的所有快捷键
export const getShortcuts = (win: BrowserWindow) => {
  const isMac = process.platform === 'darwin'
  const platform = isMac ? 'mac' : 'windows'
  
  // 合并所有快捷键组
  return shortcutGroups.reduce((acc, group) => {
    const shortcuts = group.shortcuts[platform]
    return {
      ...acc,
      ...Object.fromEntries(
        Object.entries(shortcuts).map(([key, callback]) => [
          key,
          () => callback(win)
        ])
      )
    }
  }, {} as ShortcutMap)
} 